﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using t01crm.Data;

namespace t01crm.View
{
    public class CatalogViewModel : ViewModelBase
    {
          #region Data

        readonly ObservableCollection<CatalogViewModel> _children;
        readonly CatalogViewModel _parent;
        readonly HG_Prod_Catalog _prodcatalog;

        bool _isExpanded;
        bool _isSelected;

        #endregion // Data

        #region Constructors

        public CatalogViewModel(HG_Prod_Catalog hgprodcatalog)
            : this(hgprodcatalog, null)
        {
        }

        private CatalogViewModel(HG_Prod_Catalog hgprodcatalog, CatalogViewModel parent)
        {
            _prodcatalog = hgprodcatalog;
            _parent = parent;

            _children = new ObservableCollection<CatalogViewModel>(
                    (from child in _prodcatalog.Children
                     select new CatalogViewModel(child, this))
                     .ToList<CatalogViewModel>());
        }

        #endregion // Constructors

        #region Person Properties

        public ObservableCollection<CatalogViewModel> Children
        {
            get { return _children; }
        }

        public string Name
        {
            get { return _prodcatalog.catalog_name; }
        }
        public long ID
        {
            get { return _prodcatalog.catalog_id ; }
        }
        public long FatherID
        {
            get { return _prodcatalog.catalog_father ; }
        }

        #endregion // Person Properties

        #region Presentation Members

        #region IsExpanded

        /// <summary>
        /// Gets/sets whether the TreeViewItem 
        /// associated with this object is expanded.
        /// </summary>
        public bool IsExpanded
        {
            get { return _isExpanded; }
            set
            {
                if (value != _isExpanded)
                {
                    _isExpanded = value;
                    FirePropertyChanged("IsExpanded");
                }

                // Expand all the way up to the root.
                if (_isExpanded && _parent != null)
                    _parent.IsExpanded = true;
            }
        }

        #endregion // IsExpanded

        #region IsSelected

        /// <summary>
        /// Gets/sets whether the TreeViewItem 
        /// associated with this object is selected.
        /// </summary>
        public bool IsSelected
        {
            get { return _isSelected; }
            set
            {
                if (value != _isSelected)
                {
                    _isSelected = value;
                    FirePropertyChanged("IsSelected");
                }
            }
        }

        #endregion // IsSelected

        #region NameContainsText

        //public bool NameContainsText(string text)
        //{
        //    if (String.IsNullOrEmpty(text) || String.IsNullOrEmpty(this.Name))
        //        return false;

        //    return this.Name.IndexOf(text, StringComparison.InvariantCultureIgnoreCase) > -1;
        //}

        #endregion // NameContainsText

        #region Parent

        public CatalogViewModel Parent
        {
            get { return _parent; }
        }

        #endregion // Parent

        #endregion // Presentation Members        

       
    }
}
